package com.sanrenzu.everybodytv.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.ui.column.ColumFragment;
import com.sanrenzu.everybodytv.ui.live.LiveFragment;
import com.sanrenzu.everybodytv.ui.mine.recommend.RecommendMineFragment;
import com.sanrenzu.everybodytv.ui.recommend.RecommendFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;

    private Unbinder bind;
    private RecommendFragment recommendFragment;
    private ColumFragment columFragment;
    private LiveFragment liveFragment;
    private RecommendMineFragment recommendMineFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;

    //定义选择碎片的下标
    private static final int SELECTED_RECOMMEND = 0;
    private static final int SELECTED_COLUMN = 1;
    private static final int SELECTED_LIVE = 2;
    private static final int SELECTED_MINE = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        //获取值进行接口回调传值
        initFragment();

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.btn_tabbar_home_selected,"推荐").setActiveColor(R.color.darkorange))
        .addItem(new BottomNavigationItem(R.drawable.btn_tabbar_lanmu_selected,"栏目").setActiveColor(R.color.aquamarine))
        .addItem(new BottomNavigationItem(R.drawable.btn_tabbar_zhibo_selected,"直播").setActiveColor(R.color.blue))
        .addItem(new BottomNavigationItem(R.drawable.btn_tabbar_wode_selected,"我的").setActiveColor(R.color.chartreuse))
        .initialise();

        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {

            @Override
            public void onTabSelected(int position) {
                switch (position){
                    case 0:
                        selectFragment(SELECTED_RECOMMEND);
                        break;
                    case 1:
                        selectFragment(SELECTED_COLUMN);
                        break;
                    case 2:
                        selectFragment(SELECTED_LIVE);
                        break;
                    case 3:
                        selectFragment(SELECTED_MINE);
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        selectFragment(SELECTED_RECOMMEND);


    }


    private void initFragment() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        recommendFragment = new RecommendFragment();
        columFragment = new ColumFragment();
        liveFragment = new LiveFragment();
        recommendMineFragment = new RecommendMineFragment();

        transaction.add(R.id.framelayout_main,recommendFragment);
        transaction.add(R.id.framelayout_main,columFragment);
        transaction.add(R.id.framelayout_main,liveFragment);
        transaction.add(R.id.framelayout_main, recommendMineFragment);

        transaction.commit();
    }

    private void selectFragment(int position) {
        transaction = manager.beginTransaction();
        transaction.hide(recommendFragment).hide(columFragment).hide(liveFragment).hide(recommendMineFragment);
        switch (position){
            case SELECTED_RECOMMEND:
                transaction.show(recommendFragment);
                break;
            case SELECTED_COLUMN:
                transaction.show(columFragment);
                break;
            case SELECTED_LIVE:
                transaction.show(liveFragment);
                break;
            case SELECTED_MINE:
                transaction.show(recommendMineFragment);
                break;
        }
        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //退出窗口
        if (keyCode==KeyEvent.KEYCODE_BACK){
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("确认退出？");
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.this.finish();
                }
            });
            builder.show();
        }
        return super.onKeyDown(keyCode, event);
    }


}
